package edu.umd.ion.wavemeter.client;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Widget;

public class KeyoardOnlyNumbers implements KeyPressHandler {
	private ClickHandler Handler;
	
	public KeyoardOnlyNumbers(ClickHandler handler) {
		Handler = handler;
	}
	
	public void onKeyPress(KeyPressEvent e) {
		char keyCode = e.getCharCode();
		if ((!Character.isDigit(keyCode)) && (keyCode != (char) KeyCodes.KEY_TAB)
				&& (keyCode != (char) KeyCodes.KEY_BACKSPACE)
				&& (keyCode != (char) KeyCodes.KEY_DELETE) && (keyCode != (char) KeyCodes.KEY_ENTER) 
				&& (keyCode != (char) KeyCodes.KEY_HOME) && (keyCode != (char) KeyCodes.KEY_END)
				&& (keyCode != (char) KeyCodes.KEY_LEFT) && (keyCode != (char) KeyCodes.KEY_UP)
				&& (keyCode != (char) KeyCodes.KEY_RIGHT) && (keyCode != (char) KeyCodes.KEY_DOWN)) {
		        // TextBox.cancelKey() suppresses the current keyboard event.
		        e.stopPropagation();
		}
		else if (keyCode==(char) KeyCodes.KEY_ENTER) {
			Handler.onClick(null);
		}
			
	}
	
	public void onKeyUp(Widget sender, char keyCode, int modifiers) {
	}

}
